import turtle
import math
import time

# 设置屏幕
screen = turtle.Screen()
screen.title("鳄鱼逃跑模拟")
screen.bgcolor("white")
screen.setup(width=800, height=800)
screen.tracer(0)  # 关闭自动刷新


# 绘制等边三角形
def draw_equilateral_triangle(side_length):
    t = turtle.Turtle(visible=False)
    t.speed(0)
    t.penup()
    t.goto(-side_length / 2, -side_length * math.sqrt(3) / 6)
    t.pendown()
    for _ in range(3):
        t.forward(side_length)
        t.left(120)
    return t


TRIANGLE_SIDE = 200  # 等边三角形的边长
triangle = draw_equilateral_triangle(TRIANGLE_SIDE)

# 设置人的初始位置（中心）
PERSON_RADIUS = TRIANGLE_SIDE / 2 - 50  # 大约100米到顶点的距离
person = turtle.Turtle(visible=True, shape="circle")
person.color("blue")
person.penup()
person.goto(0, -PERSON_RADIUS / math.sqrt(3))  # 调整位置到中心下方
person_speed = 10  # m/s

# 设置鳄鱼的位置
crocodiles = []
colors = ["red", "green", "orange"]
positions = [
    (-TRIANGLE_SIDE / 2, -TRIANGLE_SIDE * math.sqrt(3) / 6),  # A 左下角
    (TRIANGLE_SIDE / 2, -TRIANGLE_SIDE * math.sqrt(3) / 6),  # B 右下角
    (0, TRIANGLE_SIDE * math.sqrt(3) / 3)  # C 顶部
]

for i, pos in enumerate(positions):
    c = turtle.Turtle(visible=True, shape="circle")
    c.color(colors[i])
    c.penup()
    c.goto(pos)
    c.speed = [15, 20, 20][i]  # A:15, B:20, C:20 m/s
    crocodiles.append(c)


# 计算两点之间的距离
def distance(t1, t2):
    x1, y1 = t1.xcor(), t1.ycor()
    x2, y2 = t2.xcor(), t2.ycor()
    return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)


# 找到最近的鳄鱼
def nearest_crocodile(person, crocodiles):
    min_dist = float('inf')
    nearest = None
    for c in crocodiles:
        dist = distance(person, c)
        if dist < min_dist:
            min_dist = dist
            nearest = c
    return nearest, min_dist


# 逃跑策略：远离最近的鳄鱼
def escape(person, nearest, speed):
    x, y = person.position()
    cx, cy = nearest.position()
    # 计算方向向量
    dx = x - cx
    dy = y - cy
    # 归一化方向向量
    dist = math.sqrt(dx ** 2 + dy ** 2)
    if dist == 0:
        # 如果已经在鳄鱼位置，随机移动
        angle = math.radians(360 * (time.time() % 1))
        person.setheading(math.degrees(angle))
    else:
        # 计算单位方向向量
        unit_x = dx / dist
        unit_y = dy / dist
        # 反向移动
        new_x = x + unit_x * speed / 10  # /10 为了视觉效果更平滑
        new_y = y + unit_y * speed / 10
        person.goto(new_x, new_y)
    return


# 初始化时间
start_time = time.time()
elapsed_time = 0
time_step = 0.1  # 模拟时间步长，单位秒
safe_distance = 1  # 安全距离，当鳄鱼距离人小于等于1米时认为被捕捉

while True:
    screen.update()  # 刷新屏幕

    # 找到最近的鳄鱼
    nearest, dist = nearest_crocodile(person, crocodiles)

    # 检查是否被捕捉
    if dist <= safe_distance:
        elapsed_time = time.time() - start_time
        print(f"人被捕捉！存活时间：{elapsed_time:.2f} 秒")
        break

    # 人逃跑
    escape(person, nearest, person_speed)

    # 鳄鱼移动
    for c in crocodiles:
        cx, cy = c.position()
        px, py = person.position()
        # 计算鳄鱼到人的方向
        dx = px - cx
        dy = py - cy
        dist_c = math.sqrt(dx ** 2 + dy ** 2)
        if dist_c == 0:
            # 如果已经在人位置，随机移动
            angle = math.radians(360 * (time.time() % 1))
            c.setheading(math.degrees(angle))
        else:
            # 计算单位方向向量
            unit_x = dx / dist_c
            unit_y = dy / dist_c
            # 移动鳄鱼
            new_cx = cx + unit_x * c.speed * time_step
            new_cy = cy + unit_y * c.speed * time_step
            c.goto(new_cx, new_cy)

    # 暂停以控制模拟速度
    time.sleep(time_step)

# 保持窗口打开
screen.mainloop()